package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;

import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Category;
import com.guoguo.entity.Groups;
import com.guoguo.entity.vo.AcCategoryGroupVO;
import com.guoguo.entity.vo.CategoryVO;
import com.guoguo.entity.vo.GroupVO;
import com.guoguo.service.GroupsService;
import com.guoguo.service.ICategoryService;
import com.guoguo.utils.UUIDUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/1.
 */

@Controller
@RequestMapping("/view")
public class CategoryController extends BaseController {

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private GroupsService groupsService;

    //查询
    @RequestMapping(value = "/category", method = RequestMethod.GET)
    public ModelAndView Category(@RequestParam int pageNumber, @RequestParam int pageSize) {
        Map<String, Object> data = new HashedMap();
        PageBean<AcCategoryGroupVO> category = categoryService.getCategory(pageNumber,pageSize);
        List<Groups> group= groupsService.getgroupList();
        data.put("groupList",group);
        data.put("cateList", category);
        return returnView("/categoryList", data);
    }

    //按条件查询
    @ResponseBody
    @RequestMapping(value="/searchCategory",method=RequestMethod.POST)
    public JSONObject selectBy(@RequestParam String cateName,@RequestParam Integer isShow,@RequestParam String groupId,@RequestParam String rankType,@RequestParam String orderType,@RequestParam int pageNumber,@RequestParam int pageSize){
        Map<String,Object > data=new HashedMap() ;

        if(rankType.equals("sort") && orderType.equals("desc") ){
            PageBean<AcCategoryGroupVO> categoryGroupVOs=categoryService.selectBySortDesc(cateName ,isShow ,groupId,pageNumber,pageSize);
            data.put("cateList",categoryGroupVOs);
        }
        else if(rankType .equals("sort")&& orderType .equals("asc") ){
            PageBean<AcCategoryGroupVO> categoryGroupVOs=categoryService.selectBySortAsc(cateName ,isShow ,groupId,pageNumber,pageSize) ;
            data.put("cateList",categoryGroupVOs);
        }
        else if(rankType .equals("createTime")&& orderType .equals("desc") ){
            PageBean<AcCategoryGroupVO> categoryGroupVOs=categoryService.selectByCreateTimeDesc(cateName ,isShow ,groupId,pageNumber,pageSize) ;
            data.put("cateList",categoryGroupVOs);
        }
        else if(rankType .equals("createTime")&& orderType .equals("asc") ){
            PageBean<AcCategoryGroupVO> categoryGroupVOs=categoryService.selectByCreateTimeAsc(cateName ,isShow ,groupId,pageNumber,pageSize) ;
            data.put("cateList",categoryGroupVOs);
        }else{
            data.put("cateList",null);
        }
        return returnResult(200, data,"");
    }


    //跳转添加修改
    @RequestMapping(value = "/saveCategory", method = RequestMethod.GET)
    public ModelAndView saveCategory(String cateId) {
       Map<String,Object > data=new HashedMap();
        List<Groups> group= groupsService.getgroupList();
        data.put("groupList",group);
        if (cateId == null) {
            data.put("cateId", UUIDUtils.getUUID());
        } else {
            Category category =categoryService .selectByPrimaryKey(cateId);
            data.put("cateId",category.getCateId());
            data.put("cateName",category.getCateName() );
            data.put("cateDescription",category.getCateDescription());
            data.put("groupId",category .getGroupId() );
            data.put("sort",category.getSort());

        }
        return returnView("/category/categoryAdd", data);

    }

    /**
     * 保存添加修改
     * @param cateId
     * @param cateName
     * @param cateDescription
     * @param isShow
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateCategory", method = RequestMethod.POST)
    public JSONObject updateCategory(@RequestParam String cateId,@RequestParam String cateName,@RequestParam  String cateDescription,@RequestParam String groupId,@RequestParam int sort) {
            Category category=categoryService.addCategory(cateId,cateName,cateDescription,groupId,sort,new Date(),new Date());
       return  returnResult(200,category,"");
    }

    //删除
    @RequestMapping(value = "/deleteCategory", method = RequestMethod.GET)
    public String deleteCategory(String cateId) {
        int i = categoryService.UDByPrimaryKey(cateId);
        return "redirect:/view/category?pageNumber=1&pageSize=10";

    }



}
